//
//  UserCenterCell.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/25.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserCenterCell.h"
#import "UserItem.h"
#import "UserArrowItem.h"
#import "UIView+Frame.h"
#import "UserSaveItem.h"
#import "Masonry.h"
#import "UserLabelItem.h"
#import "UserImageItem.h"
#import "UIImageView+CM_ShowImage.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <SDWebImage/SDImageCache.h>


@interface UserCenterCell ()

// 开关
@property (nonatomic, strong) UISwitch *accessorySwitch;
// 箭头
@property (nonatomic, strong) UIImageView *accessoryArrow;
// label
@property (nonatomic, strong) UILabel *accessoryLabel;

@property (nonatomic, strong)UIView *lineView;

/** 打钩 */
@property (nonatomic, strong) UIImageView *selectImageView;

@property (nonatomic, strong) UIImageView *rightImageView; // 右侧的图片


@property (nonatomic, strong) UIView *bottomLineView; // 底部分割线
@property (nonatomic, strong) UIView *topLineView; // 顶部分割线


@end

@implementation UserCenterCell


+ (instancetype)settingViewCellTableView:(UITableView *)tableview withStyle:(UITableViewCellStyle)style {
    static NSString *ID = @"setting";
    UserCenterCell *cell = [tableview dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UserCenterCell alloc] initWithStyle:style reuseIdentifier:ID];
    }
    
    return cell;
    
}

+ (instancetype)settingViewCellTableView:(UITableView *)tableview {
    return [self settingViewCellTableView:tableview withStyle:UITableViewCellStyleDefault];
}

// 设置cell的样式
- (void)setupCell {

    self.textLabel.textColor = [UIColor colorWithHex:0x151515];
    self.textLabel.font = [UIFont systemFontOfSize:16];
    
    self.detailTextLabel.textColor = [UIColor colorWithHex:0x787878];
    self.detailTextLabel.font = [UIFont systemFontOfSize:14];
    
    self.textLabel.numberOfLines = 0;
    self.detailTextLabel.numberOfLines = 0;
    
    if ([self.item isKindOfClass:[UserImageItem class]]) {
        self.rightImageView.hidden = NO;
    }else {
        self.rightImageView.hidden = YES;
    }
}
- (void)setItem:(UserItem *)item {
    _item = item;
    // 设置数据
    // 判断有没有图片
    if(item.icon) {
        self.imageView.image = [UIImage imageNamed:item.icon];
    }
    // 如果是开关就设置成不能被选中
    self.selectionStyle = [item isKindOfClass:[UserLabelItem class]] ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleDefault;
    
    self.textLabel.text = item.title;
    self.detailTextLabel.text = item.subTitle;
  
    if ([item isKindOfClass:[UserArrowItem class]]) {
        self.accessoryView = [self accessoryArrow];
    }else if ([item isKindOfClass:[UserSaveItem class]]){
     
    }else if ([item isKindOfClass:[UserLabelItem class]]){
        // 给label赋值
        UserLabelItem *itemLabel = (UserLabelItem *)item;
        self.accessoryLabel.text = itemLabel.labelText;
    }else if ([item isKindOfClass:[UserImageItem class]]){
        [self rightImageView];
        UserImageItem *itemImage = (UserImageItem *)item;
        
        if (itemImage.image != nil) {
            self.rightImageView.image = itemImage.image;
        }else {
            [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:itemImage.imagePath] placeholderImage:[UIImage imageNamed:@"user_camera"]];
        }
    }
    
    [self setupCell];
}

- (UIImageView *)accessoryArrow {
    if (_accessoryArrow == nil) {
        _accessoryArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"user_icon_arrow"]];
    }
    return _accessoryArrow;
}
- (UILabel *)accessoryLabel {
    if (_accessoryLabel == nil) {
        _accessoryLabel = [[UILabel alloc] init];
        // 设置字体大小
        _accessoryLabel.font = [UIFont systemFontOfSize:14];
       _accessoryLabel.textColor = [UIColor colorWithHex:0x787878];
        _accessoryLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:_accessoryLabel];
        [_accessoryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.right.equalTo(self.contentView).offset(-20);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(30);
        }];
    }
    return _accessoryLabel;
}

- (UIImageView *)selectImageView {
    if (_selectImageView == nil) {
        _selectImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"user_save"]];
        [self.contentView addSubview:_selectImageView];
        [_selectImageView sizeToFit];
        
        [_selectImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.right.equalTo(self.contentView).offset(-20);
        }];
        
    }
    return _selectImageView;
}

- (UIImageView *)rightImageView {
    if (_rightImageView == nil) {
        _rightImageView = [[UIImageView alloc] init];
        
        _rightImageView.userInteractionEnabled = YES;
        
       // [_rightImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(setHeadIcon)]];
        
        
        [self.contentView addSubview:_rightImageView];
        
    }
    return _rightImageView;
}

//- (void)setHeadIcon {
//    [self.rightImageView showImage];
//}

#pragma mark 添加分隔线
- (UIView *)lineView {
    if(_lineView == nil) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kLineBackgroundColor;
        _lineView.hidden = YES;
        [self.contentView addSubview:_lineView];
    }
    return _lineView;
}

- (UIView *)bottomLineView {
    if (_bottomLineView == nil) {
        _bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, kAutoHei(44) - 0.5, kYFScreenWidth, 0.5)];
        _bottomLineView.backgroundColor = kLineBackgroundColor;
        [self.contentView addSubview:_bottomLineView];
    }
    return _bottomLineView;
}
- (UIView *)topLineView {
    if (_topLineView == nil) {
        _topLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kYFScreenWidth, 0.5)];
        _topLineView.backgroundColor = kLineBackgroundColor;
        [self.contentView addSubview:_topLineView];
    }
    return _topLineView;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.bottomLineView.y = self.bounds.size.height - 0.5;
    // 设置分隔线的frame
    if (self.item.icon) {
        self.lineView.x = self.imageView.x;
    }else {
        self.lineView.x = self.textLabel.x;
    }
    self.lineView.y = self.bounds.size.height - 0.5;
    self.lineView.width = self.bounds.size.width;
    self.lineView.height = 0.5;
    
    if ([self.item isKindOfClass:[UserImageItem class]]) {
        
        CGFloat margin = 7;
        CGFloat imageWH = self.height - margin * 2;
        self.rightImageView.x = self.width - imageWH - 16;
        self.rightImageView.width = imageWH;
        self.rightImageView.height = imageWH;
        self.rightImageView.y = margin;
        self.rightImageView.layer.cornerRadius = imageWH * 0.5;
        self.rightImageView.layer.masksToBounds = YES;
    }
}

- (void)showLine:(BOOL)show {
    self.lineView.hidden = !show;
    self.bottomLineView.hidden = show;
}
- (void)showSectionSepTopLineView:(BOOL)show {
    self.topLineView.hidden = !show;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    if ([self.item isKindOfClass:[UserSaveItem class]]) {
        if (self.selected) {
            self.selectImageView.hidden = NO;
        }else {
            self.selectImageView.hidden = YES;
        }
    }
}


@end
